/**
 * Copyright © 2021-2025 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.servicecenter.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.jeeplus.aop.logging.annotation.ApiLog;
import com.jeeplus.core.excel.EasyExcelUtils;
import com.jeeplus.core.excel.ExcelOptions;
import com.jeeplus.core.excel.annotation.ExportMode;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.servicecenter.domain.ActivityStatistics;
import com.jeeplus.servicecenter.service.ActivityStatisticsService;
import com.jeeplus.servicecenter.service.dto.ActivityStatisticsDTO;
import com.jeeplus.servicecenter.service.mapstruct.ActivityStatisticsWrapper;
import com.jeeplus.sys.domain.User;
import com.jeeplus.sys.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * 活跃度统计Controller
 * @author 淋漓
 * @version 2024-05-26
 */

@Api(tags ="活跃度统计")
@RestController
@RequestMapping(value = "/seCenter/statistics")
public class ActivityStatisticsController {

	@Autowired
	private ActivityStatisticsService activityStatisticsService;

	@Autowired
	private ActivityStatisticsWrapper activityStatisticsWrapper;

	@Autowired
	private UserService userService;

//	/**
//	 * 活跃度列表数据
//	 */
//	@ApiLog("查询活跃度列表数据")
//	@ApiOperation(value = "查询活跃度列表数据")
//	@PreAuthorize("hasAuthority('servicecenter:activityStatistics:list')")
//	@GetMapping("list")
//	public ResponseEntity<IPage<ActivityStatisticsDTO>> list(ActivityStatisticsDTO activityStatisticsDTO, Page<ActivityStatistics> page) throws Exception {
//		QueryWrapper queryWrapper = QueryWrapperGenerator.buildQueryCondition (activityStatisticsDTO, ActivityStatisticsDTO.class);
//		IPage<ActivityStatisticsDTO> result = activityStatisticsWrapper.toDTO ( activityStatisticsService.page (page, queryWrapper) );
//		return ResponseEntity.ok (result);
//	}


	/**
	 * 根据Id获取活跃度数据
	 */
	@ApiLog("根据Id获取活跃度数据")
	@ApiOperation(value = "根据Id获取活跃度数据")
//	@PreAuthorize("hasAnyAuthority('seCenter:statistics:view')")
	@GetMapping("queryById")
	public ResponseEntity<ActivityStatisticsDTO> queryById(String id) {
		return ResponseEntity.ok ( activityStatisticsWrapper.toDTO ( activityStatisticsService.getById ( id ) ) );
	}

	/**
	 * 保存活跃度
	 */
	@ApiLog("保存活跃度")
	@ApiOperation(value = "保存活跃度")
	@PreAuthorize("hasAnyAuthority('seCenter:statistics:save')")
	@PostMapping("save")
	public  ResponseEntity <String> save(@Valid @RequestBody ActivityStatisticsDTO activityStatisticsDTO) {
		User user = userService.getOne(new QueryWrapper<User>().eq("login_name", activityStatisticsDTO.getLoginName()).eq("del_flag","0"));
		if(user!=null){
			activityStatisticsDTO.setId(user.getId());
		}
		//新增或编辑表单保存
		activityStatisticsService.saveOrUpdate (activityStatisticsWrapper.toEntity (activityStatisticsDTO));
        return ResponseEntity.ok ( "保存活跃度成功" );
	}


	/**
	 * 删除活跃度
	 */
	/*@ApiLog("删除活跃度")
	@ApiOperation(value = "删除活跃度")
	@PreAuthorize("hasAuthority('servicecenter:activityStatistics:del')")
	@DeleteMapping("delete")
	public ResponseEntity <String> delete(String ids) {
		String idArray[] = ids.split(",");
        activityStatisticsService.removeByIds ( Lists.newArrayList ( idArray ) );
		return ResponseEntity.ok( "删除活跃度成功" );
	}*/
	
    /**
     * 导出活跃度数据
     *
     * @param activityStatisticsDTO
     * @param page
     * @param response
     * @throws Exception
     */
   /* @ApiLog("导出活跃度数据")
    @PreAuthorize("hasAnyAuthority('servicecenter:activityStatistics:export')")
    @GetMapping("export")
    public void exportFile(ActivityStatisticsDTO activityStatisticsDTO, Page <ActivityStatistics> page, ExcelOptions options, HttpServletResponse response) throws Exception {
        String fileName = options.getFilename ( );
		QueryWrapper queryWrapper = QueryWrapperGenerator.buildQueryCondition (activityStatisticsDTO, ActivityStatisticsDTO.class);
        if ( ExportMode.current.equals ( options.getMode ( ) ) ) { // 导出当前页数据
            
        } else if ( ExportMode.selected.equals ( options.getMode ( ) ) ) { // 导出选中数据
            queryWrapper.in ( "id", options.getSelectIds () );
        } else { // 导出全部数据
            page.setSize ( -1 );
            page.setCurrent ( 0 );
        }
        List < ActivityStatistics> result = activityStatisticsService.page ( page, queryWrapper ).getRecords ( );
        EasyExcelUtils.newInstance ( activityStatisticsService, activityStatisticsWrapper ).exportExcel ( result,  options.getSheetName ( ), ActivityStatisticsDTO.class, fileName,options.getExportFields (), response );
    }*/

    /**
     * 导入活跃度数据
     *
     * @return
     */
    /*@PreAuthorize("hasAnyAuthority('servicecenter:activityStatistics:import')")
    @PostMapping("import")
    public ResponseEntity importFile(MultipartFile file) throws IOException {
        String result = EasyExcelUtils.newInstance ( activityStatisticsService, activityStatisticsWrapper ).importExcel ( file, ActivityStatisticsDTO.class );
        return ResponseEntity.ok ( result );
    }*/

    /**
     * 下载导入活跃度数据模板
     *
     * @param response
     * @return
     */
    /*@PreAuthorize ("hasAnyAuthority('servicecenter:activityStatistics:import')")
    @GetMapping("import/template")
    public void importFileTemplate(HttpServletResponse response) throws IOException {
        String fileName = "活跃度数据导入模板.xlsx";
        List<ActivityStatisticsDTO> list = Lists.newArrayList();
        EasyExcelUtils.newInstance ( activityStatisticsService, activityStatisticsWrapper ).exportExcel ( list,  "活跃度数据", ActivityStatisticsDTO.class, fileName, null, response );
    }*/


}
